package com.gy.smart.util;

import android.content.Context;
import android.os.IBinder;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

/**
 * Created by 高岳 on 2016/6/30.
 * Describe:键盘处理 显示和隐藏
 */
public class KeyBoard {

    private static InputMethodManager imm;

    /**
     * 键盘隐藏
     *
     * @param c
     * @param e
     */
    public static void dismissKeyBoard(Context c, EditText e) {
        if (imm == null)
            imm = (InputMethodManager) c
                    .getSystemService(Context.INPUT_METHOD_SERVICE);

        imm.hideSoftInputFromWindow(e.getWindowToken(), 0);
        e.clearFocus();
    }

    public static void dismissKeyBoard(Context c, IBinder token){

        if (imm == null){
            imm = (InputMethodManager)c.getSystemService(Context.INPUT_METHOD_SERVICE);
        }
        if (imm.isActive()){
            imm.hideSoftInputFromWindow(token,0);
        }

    }


    /**
     * 键盘显示
     *
     * @param c
     * @param e
     */
    public static void showKeyBoard(Context c, View e) {
        if (imm == null)
            imm = (InputMethodManager) c
                    .getSystemService(Context.INPUT_METHOD_SERVICE);
        // 如果开启
        imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT,
                InputMethodManager.HIDE_NOT_ALWAYS);

    }

}
